<?php

class BShopOrder extends BaseModel
{
    public $res;
    public $expire;

    public function getSource()
    {
        return "b_shop_order";
    }

    public function getOrder($no)
    {
        $info = $this->findFirstByOrderNo($no);
        if (!$info) {
            return '订单不存在!';
        }
        $skuDb   = new BShopGoodsSku();
        $skuInfo = $skuDb->findFirstById($info->sku_id);
        if (!$skuInfo) {
            return 'sku_不存在!';
        }
        $goodsDb   = new BShopGoods();
        $goodsInfo = $goodsDb->findFirstById($info->goods_id);
        if (!$goodsInfo) {
            return '商品不存在!';
        }
        $valueDb                = new BShopSkuValue();
        $result['order_no']     = $info->order_no;
        $result['num']          = $info->num;
        $result['price']        = $info->price;
        $result['total_price']  = $info->total_price;
        $result['growth_value'] = $info->growth_value;
        $result['status']       = $info->order_status;
        $result['order_status'] = CommonController::ORDER_STATUS[$info->order_status];
        $result['goods_img']    = $skuInfo->imageUrl;
        $result['goods_name']   = $goodsInfo->name;
        $result['goods_id']     = $goodsInfo->id;
        $result['sku_value']    = $valueDb->getValueName($skuInfo->value_ids);
        return $result;
    }

    public function returnOrder($data)
    {
        $where = "user_id = {$data['user_id']} and order_no =  '{$data['order_no']}'";
        $info  = $this->findFirst($where);
        if ($info) {
            if (!in_array($info->order_status, [2, 4])) {
                return '此订单状态不能退货';
            }
            $info->order_status = 11;
            $info->updated_at   = date('Y-m-d H:i:s');
            if (!$info->save()) {
                return '订单状态修改失败!';
            }
            return true;
        } else {
            return '订单不存在';
        }
    }
}
